package com.csdj.shop.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

/**
 * author:cloud
 * date:2021/2/26 0026
 **/
public class MybatisUtil {
    private static SqlSessionFactory factory = null;

    /**
     * 获取sqlsession
     * @param autocommit 是否自动提交
     * @return
     */
    public static SqlSession getSession(boolean autocommit){
        if(factory == null){
            try {
                factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return factory.openSession(autocommit);
    }
}
